//
//  SettingVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/4.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "MoreSettingVC.h"
#import "MicroDef.h"
#import "SettingNormalCell.h"
#import "SettingUserInfoCell.h"
#import "AboutVC.h"

#import "SettingVC.h"
#import "filesManagerVC.h"
#import "SignatureVC.h"
#import "MyInformationVC.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "UIImageView+WebCache.h"
#import "CXmppRequestMgr.h"
#import "ChildHoodMessage.h"
#import "CFolderMgr.h"
#import "CHChatRoomBase.h"
#import "EFengMineAttentionController.h"
#import "EFengPersonalShareController.h"

#define KSECTIONHEIGHT 20.0f
#define KHEADERCELLHEIGHT 75.0f
#define KNORMALCELLHEIGHT 50.0f

@interface MoreSettingVC ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *_tableView;
//    NSArray *_titlesArray;
//    NSArray *_iconsArray;
}

@property (nonatomic, strong) CContactModel *curContact;
@property (nonatomic, strong) UIImageView *headerImageView;
@end

@implementation MoreSettingVC

- (void)dealloc
{
    NSLog(@"moreSettingVc");
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"我"];
    
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f - 49.0f) style:UITableViewStyleGrouped];
//    CGFloat height = KHEADERCELLHEIGHT + KNORMALCELLHEIGHT*4 + KSECTIONHEIGHT*3;
//    _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, height);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(44, 170, 253)];
    } else {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(44, 170, 253)];
    }

    //    self.curContact = [[[CContactMgr alloc] init] loadCurContact];
    self.curContact = [[[CContactMgr alloc] init] loadCurContact];
    [_tableView reloadData];
    
}



//-(void)createShowInfo
//{
//    _titlesArray = [NSArray arrayWithObjects:@"", @"个人签名", @"我的文件", @"设置", @"关于", nil];
//    _iconsArray = [NSArray arrayWithObjects:@"",@"signatureIcon", @"fileManagerIcon", @"settingIcon", @"aboutIcon", nil];
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger rows = 0;
    
    switch (section) {
        case 0:
            rows = 1;
            break;
        case 1:
            rows = 6;
            break;
        case 2:
            rows = 1;
            break;
        case 3:
            rows = 1;
            break;
        default:
            rows = 0;
            break;
    }
    return rows;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    return 4;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettingUserInfoCell";
    static NSString *NormalCellIdentifier = @"SettingNormalCell";
    UITableViewCell *cell = nil;
    
    if(indexPath.section == 0)
    {
//        SettingUserInfoCell.
        
        SettingUserInfoCell *userCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (userCell == nil) {
            userCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingUserInfoCell" owner:self options:nil] objectAtIndex:0];
        }
        [userCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:self.curContact.UserID]] placeholderImage:[UIImage imageNamed:@"110-110"]];
//        [userCell.icon setImage:[UIImage imageNamed:@"defaultHeaderImage"]];
        [userCell.name setText:self.curContact.UserName];
        [userCell.job setText:self.curContact.Position];
        
        cell = userCell;
    }
    else
    {
        SettingNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
        if (normalCell == nil) {
            normalCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingNormalCell" owner:self options:nil] objectAtIndex:0];
        }
        
        NSInteger numCount = [tableView numberOfRowsInSection:indexPath.section];
        [normalCell.icon setImage:[UIImage imageNamed:[self iconName:indexPath]] forState:UIControlStateNormal];
        [normalCell.titleLabel setText:[self titleName:indexPath]];
        if(numCount - 1 == indexPath.row)
        {
            [normalCell.seperatorLine setHidden:YES];
        }
        else
        {
            [normalCell.seperatorLine setHidden:NO];
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell = normalCell;
    }
    return cell;
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"个人签名";
                    break;
                case 1:
                    title = @"我的文件";
                    break;
                case 2:
                    title = @"我的电脑";
                    break;
                case 3:
                    title = @"我的关注";
                    break;
                case 4:
                    title = @"我的分享";
                    break;
                case 5:
                    title = @"我的钱包";
                    break;
                case 6:
                    title = @"我的红包";
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            title = @"设置";
        }
            break;
        case 3:
        {
            title = @"关于";
        }
            break;
        default:
            break;
    }
    return title;
}

-(NSString *)iconName:(NSIndexPath *)indexPath
{
    NSString *iconName = nil;
    
    switch (indexPath.section) {
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    iconName = @"signatureIcon";
                    break;
                case 1:
                    iconName = @"fileManagerIcon";
                    break;
                case 2:
                    iconName = @"icon-iMAC_file";
                    break;
                case 3:
                    iconName = @"mineAttention_star";
                    break;
                case 4:
                    iconName = @"mineShare－icon";
                    break;

                default:
                    break;
            }
        }
            break;
        case 2:
        {
            iconName = @"settingIcon";
        }
            break;
        case 3:
        {
            iconName = @"aboutIcon";
        }
            break;
        default:
            break;
    }
    return iconName;
}



#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    CGFloat height = 0.01f;
    switch (section) {
        case 1:
            height = KSECTIONHEIGHT;
            break;
        case 2:
            height = KSECTIONHEIGHT;
            break;
        case 3:
            height = KSECTIONHEIGHT;
            break;
        default:
            break;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0.01f;
    switch (section) {
        case 3:
            height = 0.01f;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
    if(section == 0)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    UIView *endLine = [[UIView alloc] init];
    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:endLine];
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 60.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 75.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    switch (indexPath.section) {
        case 0:
        {
//            [self showMyInfoVC];
//            NSString *userIdString = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId;
//            CContactModel *model;
//            model = [[[CContactMgr alloc] init] loadContactWithUserId:userIdString];
            MyInformationVC *myInfo = [[MyInformationVC alloc] init];
//            myInfo.curuser = model;
            nav.navigationBarHidden = NO;
            self.navigationController.navigationBarHidden = YES;

            [nav pushViewController:myInfo animated:YES];
            
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [self showSignatureVC];
                }
                    break;
                case 1:
                {
                    [self showFilesManagerVC];
                }
                    break;
                case 2:
                {
                    [self showChatVc];
                }
                    break;
                case 3:
                {
                    nav.navigationBarHidden = NO;
                    self.navigationController.navigationBarHidden = YES;

                    EFengMineAttentionController *attention = [[EFengMineAttentionController alloc] init];
                    [nav pushViewController:attention animated:YES];
                }
                    break;
                case 4:
                {
                    EFengPersonalShareController *per = [[EFengPersonalShareController alloc] init];
                    per.userId = self.curContact.UserID;
                    
                    nav.navigationBarHidden = NO;
                    self.navigationController.navigationBarHidden = YES;

                    [nav pushViewController:per animated:YES];
                }
                    break;

                default:
                    break;
            }
        }
            break;
            
            
        case 2:
        {
            [self showSettingVC];
        }
            break;
        case 3:
        {
            [self showAboutVC];
        }
            break;
        default:
            break;
    }


}

- (void)showChatVc
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    CHChatRoomBase *chat = [[CHChatRoomBase alloc] init];
    chat.member = self.curContact;
    chat.group = nil;
//    chat.isAPP_Pc = YES;
    [nav pushViewController:chat animated:YES];
    //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
}
    
-(void)showAboutVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    AboutVC *about = [[AboutVC alloc] init];
    [nav pushViewController:about animated:YES];
    //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
}

-(void)showSettingVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    SettingVC *setting = [[SettingVC alloc] init];
//    CHSNavigationController *settingNav = [[CHSNavigationController alloc] initWithRootViewController:setting];
    [nav pushViewController:setting animated:YES];
//    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
    }

-(void)showFilesManagerVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    filesManagerVC *filesManager = [[filesManagerVC alloc] init];
//    CHSNavigationController *filesManagerNav = [[CHSNavigationController alloc] initWithRootViewController:filesManager];
    [nav pushViewController:filesManager animated:YES];
    //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
}

-(void)showSignatureVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    SignatureVC *signature = [[SignatureVC alloc] init];
    signature.curUser = self.curContact;
//    CHSNavigationController *filesManagerNav = [[CHSNavigationController alloc] initWithRootViewController:signature];
    [nav pushViewController:signature animated:YES];
    //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
    
}

//MyInformationVC
-(void)showMyInfoVC
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    MyInformationVC *myInfo = [[MyInformationVC alloc] init];
//    CHSNavigationController *myInfoNav = [[CHSNavigationController alloc] initWithRootViewController:myInfo];
    [nav pushViewController:myInfo animated:YES];
    //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.transform =  CGAffineTransformMakeTranslation(HARDWARE_SCREEN_WIDTH, 0);
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
        cell.transform = CGAffineTransformIdentity;
    } completion:NULL];
}

@end
